<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>Introduction_to_Algorithms: src/graph_algorithms/basic_graph/graph_representation/matrix_graph/matrixgraph.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Introduction_to_Algorithms
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('matrixgraph_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">matrixgraph.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="matrixgraph_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * Copyright 2016- huaxz &lt;huaxz1986@163.com&gt;</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * This program is free software: you can redistribute it and/or modify</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> * it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * the Free Software Foundation, either version 2 of the License, or</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> * (at your option) any later version.</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> * This program is distributed in the hope that it will be useful,</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> * GNU General Public License for more details.</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment"> * You should have received a copy of the GNU General Public License</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment"> * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment"> * Author: huaxz1986@163.com (huaxz)</span></div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#ifndef MATRIXGRAPH</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="preprocessor">#define MATRIXGRAPH</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#include&lt;array&gt;</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="preprocessor">#include&lt;map&gt;</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;</div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;{</div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;    <span class="keyword">namespace </span>GraphAlgorithm</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;    {</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;</div>
<div class="line"><a name="l00037"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html">   37</a></span>&#160;       <span class="keyword">template</span>&lt;<span class="keywordtype">unsigned</span> N&gt; <span class="keyword">struct </span><a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html">MatrixGraph</a></div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;        {</div>
<div class="line"><a name="l00039"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae97378c88e8d65a880334f31a352e71d">   39</a></span>&#160;            <span class="keyword">typedef</span> <span class="keywordtype">int</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae97378c88e8d65a880334f31a352e71d">VIDType</a>;     </div>
<div class="line"><a name="l00040"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#af54dbf6d171b89b81b490de13f1fb02f">   40</a></span>&#160;            <span class="keyword">typedef</span> <span class="keywordtype">int</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#af54dbf6d171b89b81b490de13f1fb02f">EWeightType</a>; </div>
<div class="line"><a name="l00041"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ad698e07bacdb3f332c4f5d34bdb49463">   41</a></span>&#160;            <span class="keyword">typedef</span> std::tuple&lt;VIDType,VIDType,EWeightType&gt; <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ad698e07bacdb3f332c4f5d34bdb49463">EdgeTupleType</a>; </div>
<div class="line"><a name="l00042"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a68d153cb2714464d5c432d667e29f28f">   42</a></span>&#160;            <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a68d153cb2714464d5c432d667e29f28f">NUM</a>=N; </div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;</div>
<div class="line"><a name="l00047"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#abe357f589d4b46be1cbb8b36d7434681">   47</a></span>&#160;            <span class="keyword">explicit</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#abe357f589d4b46be1cbb8b36d7434681">MatrixGraph</a>(EWeightType val):<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#accc79376e509d7ffa016ef5003512976">invalid_weight</a>(val)</div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;            {</div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;                <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> i=0;i&lt;N;i++)</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;                    <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> j=0;j&lt;N;j++)</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;                        <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">matrix</a>.at(i).at(j)=<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#accc79376e509d7ffa016ef5003512976">invalid_weight</a>;</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;            }</div>
<div class="line"><a name="l00054"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a5e0154a5eed096fe10a03519022fd24d">   54</a></span>&#160;            <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a5e0154a5eed096fe10a03519022fd24d">MatrixGraph</a>():<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html">MatrixGraph</a>(0)</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;            {}</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;</div>
<div class="line"><a name="l00066"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a91283bb66d4daa938cd521d348e00984">   66</a></span>&#160;            <span class="keywordtype">void</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a91283bb66d4daa938cd521d348e00984">add_edge</a>(<span class="keyword">const</span> EdgeTupleType&amp; edge_tuple)</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;            {</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;                <span class="keyword">auto</span> id1=std::get&lt;0&gt;(edge_tuple);</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;                <span class="keyword">auto</span> id2=std::get&lt;1&gt;(edge_tuple);</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;                <span class="keyword">auto</span> wt=std::get&lt;2&gt;(edge_tuple);</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;                <span class="keywordflow">if</span>(id1&lt;0||id1&gt;=N||id2&lt;0||id2&gt;=N)</div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;                    <span class="keywordflow">return</span>;</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;                <span class="keywordflow">if</span>(<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ab5eb0a563c39a088d16e349cb3d73a2d">has_edge</a>(id1,id2))</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;edge add error,edge has already exist.&quot;</span>);</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;                <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">matrix</a>.at(id1).at(id2)=wt;</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;            }</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;</div>
<div class="line"><a name="l00087"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a0fac196d4c47c2855267eb2cf888ef14">   87</a></span>&#160;            <span class="keyword">template</span>&lt;<span class="keyword">typename</span> Iteator&gt; <span class="keywordtype">void</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a0fac196d4c47c2855267eb2cf888ef14">add_edges</a>(<span class="keyword">const</span> Iteator&amp;begin ,<span class="keyword">const</span> Iteator&amp;end)</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;            {</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;                <span class="keywordflow">if</span>(std::distance(begin,end)&lt;=0)</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;                    <span class="keywordflow">return</span>;</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;                <span class="keyword">auto</span> iter=begin;</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;                <span class="keywordflow">while</span>(iter!=end)</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;                {</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;                    <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a91283bb66d4daa938cd521d348e00984">add_edge</a>(*iter);</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;                    iter++;</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;                }</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;            }</div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;</div>
<div class="line"><a name="l00108"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#aa16d1f2e6949d0adccc30b0704013e6d">  108</a></span>&#160;            <span class="keywordtype">void</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#aa16d1f2e6949d0adccc30b0704013e6d">adjust_edge</a>(VIDType id1,VIDType id2,EWeightType wt)</div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;            {</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;                <span class="keywordflow">if</span>(id1&lt;0||id1&gt;=N||id2&lt;0||id2&gt;=N)</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;                    <span class="keywordflow">return</span>;</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;                <span class="keywordflow">if</span>(!<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ab5eb0a563c39a088d16e349cb3d73a2d">has_edge</a>(id1,id2))</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;edge adjust error,edge does not exist.&quot;</span>);</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;                <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">matrix</a>.at(id1).at(id2)=wt;</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;            }</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;</div>
<div class="line"><a name="l00121"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a2bad3638c9ecb614f65b19fe155f5cd7">  121</a></span>&#160;            <span class="keyword">const</span> std::vector&lt;EdgeTupleType&gt; <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a2bad3638c9ecb614f65b19fe155f5cd7">edge_tuples</a>()<span class="keyword"> const</span></div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;<span class="keyword">            </span>{</div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;                std::vector&lt;EdgeTupleType&gt; result;</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;                <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> i=0;i&lt;N;i++)</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;                    <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> j=0;j&lt;N;j++)</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;                    {</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;                        <span class="keywordtype">int</span> val=<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">matrix</a>.at(i).at(j);</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;                        <span class="keywordflow">if</span>(val!=<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#accc79376e509d7ffa016ef5003512976">invalid_weight</a>)</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;                            result.push_back(std::make_tuple(i,j,val));</div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;                    }</div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;                <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;            }</div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;</div>
<div class="line"><a name="l00141"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a906d21b803bfae5095066df1e31c891c">  141</a></span>&#160;            <span class="keyword">const</span> std::vector&lt;EdgeTupleType&gt; <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a906d21b803bfae5095066df1e31c891c">vertex_edge_tuples</a>(VIDType <span class="keywordtype">id</span>)<span class="keyword"> const</span></div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;<span class="keyword">            </span>{</div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;                <span class="keywordflow">if</span>(id&lt;0||id&gt;=N)</div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;                {</div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;vertex_edge_tuples: id must belongs [0,N),&quot;</span>);</div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;                }</div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;                std::vector&lt;EdgeTupleType&gt; result;</div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;                <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> j=0;j&lt;N;j++)</div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;                {</div>
<div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;                        <span class="keywordtype">int</span> val=<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">matrix</a>.at(<span class="keywordtype">id</span>).at(j);</div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;                        <span class="keywordflow">if</span>(val!=<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#accc79376e509d7ffa016ef5003512976">invalid_weight</a>)</div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;                            result.push_back(std::make_tuple(<span class="keywordtype">id</span>,j,val));</div>
<div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;                }</div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;                <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;            }</div>
<div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;</div>
<div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160;</div>
<div class="line"><a name="l00168"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ab5eb0a563c39a088d16e349cb3d73a2d">  168</a></span>&#160;            <span class="keywordtype">bool</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ab5eb0a563c39a088d16e349cb3d73a2d">has_edge</a>(VIDType id_from,VIDType id_to)<span class="keyword"> const</span></div>
<div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;<span class="keyword">            </span>{</div>
<div class="line"><a name="l00170"></a><span class="lineno">  170</span>&#160;                <span class="keywordflow">if</span>(id_from&lt;0||id_from&gt;=N||id_to&lt;0||id_to&gt;=N)</div>
<div class="line"><a name="l00171"></a><span class="lineno">  171</span>&#160;                {</div>
<div class="line"><a name="l00172"></a><span class="lineno">  172</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;has_edge: id_from  and id _to must belongs [0,N),&quot;</span>);</div>
<div class="line"><a name="l00173"></a><span class="lineno">  173</span>&#160;                }</div>
<div class="line"><a name="l00174"></a><span class="lineno">  174</span>&#160;                <span class="keywordflow">return</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">matrix</a>.at(id_from).at(id_to)!=<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#accc79376e509d7ffa016ef5003512976">invalid_weight</a>;</div>
<div class="line"><a name="l00175"></a><span class="lineno">  175</span>&#160;            }</div>
<div class="line"><a name="l00176"></a><span class="lineno">  176</span>&#160;</div>
<div class="line"><a name="l00178"></a><span class="lineno">  178</span>&#160;</div>
<div class="line"><a name="l00189"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a9304b6a4c45f368452f6ff9065334536">  189</a></span>&#160;            EWeightType <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a9304b6a4c45f368452f6ff9065334536">weight</a>(VIDType id_from,VIDType id_to)<span class="keyword"> const</span></div>
<div class="line"><a name="l00190"></a><span class="lineno">  190</span>&#160;<span class="keyword">            </span>{</div>
<div class="line"><a name="l00191"></a><span class="lineno">  191</span>&#160;                <span class="keywordflow">if</span>(<a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ab5eb0a563c39a088d16e349cb3d73a2d">has_edge</a>(id_from,id_to))</div>
<div class="line"><a name="l00192"></a><span class="lineno">  192</span>&#160;                {</div>
<div class="line"><a name="l00193"></a><span class="lineno">  193</span>&#160;                    <span class="keywordflow">return</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">matrix</a>.at(id_from).at(id_to);</div>
<div class="line"><a name="l00194"></a><span class="lineno">  194</span>&#160;                }<span class="keywordflow">else</span></div>
<div class="line"><a name="l00195"></a><span class="lineno">  195</span>&#160;                {</div>
<div class="line"><a name="l00196"></a><span class="lineno">  196</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;weight error: the edge does not exist.&quot;</span>);</div>
<div class="line"><a name="l00197"></a><span class="lineno">  197</span>&#160;                }</div>
<div class="line"><a name="l00198"></a><span class="lineno">  198</span>&#160;            }</div>
<div class="line"><a name="l00199"></a><span class="lineno">  199</span>&#160;</div>
<div class="line"><a name="l00200"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">  200</a></span>&#160;            std::array&lt;std::array&lt;EWeightType,N&gt;, N&gt;  <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">matrix</a>;  </div>
<div class="line"><a name="l00201"></a><span class="lineno"><a class="line" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#accc79376e509d7ffa016ef5003512976">  201</a></span>&#160;            <span class="keyword">const</span> EWeightType <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#accc79376e509d7ffa016ef5003512976">invalid_weight</a>;                  </div>
<div class="line"><a name="l00202"></a><span class="lineno">  202</span>&#160;        };</div>
<div class="line"><a name="l00203"></a><span class="lineno">  203</span>&#160;    }</div>
<div class="line"><a name="l00204"></a><span class="lineno">  204</span>&#160;}</div>
<div class="line"><a name="l00205"></a><span class="lineno">  205</span>&#160;<span class="preprocessor">#endif //MATRIXGRAPH</span></div>
<div class="line"><a name="l00206"></a><span class="lineno">  206</span>&#160;</div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_a9304b6a4c45f368452f6ff9065334536"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a9304b6a4c45f368452f6ff9065334536">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::weight</a></div><div class="ttdeci">EWeightType weight(VIDType id_from, VIDType id_to) const </div><div class="ttdoc">weight:返回图中指定顶点之间的边的权重 </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00189">matrixgraph.h:189</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_ae1febbc0e4a86c325a16a942947a03bf"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae1febbc0e4a86c325a16a942947a03bf">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::matrix</a></div><div class="ttdeci">std::array&lt; std::array&lt; EWeightType, N &gt;, N &gt; matrix</div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00200">matrixgraph.h:200</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_af54dbf6d171b89b81b490de13f1fb02f"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#af54dbf6d171b89b81b490de13f1fb02f">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::EWeightType</a></div><div class="ttdeci">int EWeightType</div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00040">matrixgraph.h:40</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_a906d21b803bfae5095066df1e31c891c"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a906d21b803bfae5095066df1e31c891c">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::vertex_edge_tuples</a></div><div class="ttdeci">const std::vector&lt; EdgeTupleType &gt; vertex_edge_tuples(VIDType id) const </div><div class="ttdoc">vertex_edge_tuples:返回图中从指定顶点出发的边的三元素元组集合，这里集合采用std::vector<std::tuple<VID...</div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00141">matrixgraph.h:141</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_abe357f589d4b46be1cbb8b36d7434681"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#abe357f589d4b46be1cbb8b36d7434681">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::MatrixGraph</a></div><div class="ttdeci">MatrixGraph(EWeightType val)</div><div class="ttdoc">显式构造函数，指定invalid_weight </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00047">matrixgraph.h:47</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_a68d153cb2714464d5c432d667e29f28f"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a68d153cb2714464d5c432d667e29f28f">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::NUM</a></div><div class="ttdeci">static const unsigned NUM</div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00042">matrixgraph.h:42</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_a2bad3638c9ecb614f65b19fe155f5cd7"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a2bad3638c9ecb614f65b19fe155f5cd7">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::edge_tuples</a></div><div class="ttdeci">const std::vector&lt; EdgeTupleType &gt; edge_tuples() const </div><div class="ttdoc">edge_tuples:返回图中所有边的三元素元组集合，这里集合采用std::vector<std::tuple<VIDType,VIDType,EWeightType>> </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00121">matrixgraph.h:121</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_a5e0154a5eed096fe10a03519022fd24d"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a5e0154a5eed096fe10a03519022fd24d">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::MatrixGraph</a></div><div class="ttdeci">MatrixGraph()</div><div class="ttdoc">默认构造函数，指定invalid_weight为0 </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00054">matrixgraph.h:54</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_ae97378c88e8d65a880334f31a352e71d"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ae97378c88e8d65a880334f31a352e71d">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::VIDType</a></div><div class="ttdeci">int VIDType</div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00039">matrixgraph.h:39</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_ab5eb0a563c39a088d16e349cb3d73a2d"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ab5eb0a563c39a088d16e349cb3d73a2d">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::has_edge</a></div><div class="ttdeci">bool has_edge(VIDType id_from, VIDType id_to) const </div><div class="ttdoc">has_edge:返回图中指定顶点之间是否存在边 </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00168">matrixgraph.h:168</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_accc79376e509d7ffa016ef5003512976"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#accc79376e509d7ffa016ef5003512976">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::invalid_weight</a></div><div class="ttdeci">const EWeightType invalid_weight</div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00201">matrixgraph.h:201</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_aa16d1f2e6949d0adccc30b0704013e6d"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#aa16d1f2e6949d0adccc30b0704013e6d">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::adjust_edge</a></div><div class="ttdeci">void adjust_edge(VIDType id1, VIDType id2, EWeightType wt)</div><div class="ttdoc">adjust_edge:修改一条边的权重 </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00108">matrixgraph.h:108</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_ad698e07bacdb3f332c4f5d34bdb49463"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#ad698e07bacdb3f332c4f5d34bdb49463">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::EdgeTupleType</a></div><div class="ttdeci">std::tuple&lt; VIDType, VIDType, EWeightType &gt; EdgeTupleType</div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00041">matrixgraph.h:41</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph</a></div><div class="ttdoc">MatrixGraph：图的矩阵表示，算法导论22章22.1节 </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00037">matrixgraph.h:37</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_html"><div class="ttname"><a href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div><div class="ttdoc">Namespace of IntrodunctionToAlgorithm. </div><div class="ttdef"><b>Definition:</b> <a href="longest__common__subsequence_8h_source.html#l00024">longest_common_subsequence.h:24</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_a91283bb66d4daa938cd521d348e00984"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a91283bb66d4daa938cd521d348e00984">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::add_edge</a></div><div class="ttdeci">void add_edge(const EdgeTupleType &amp;edge_tuple)</div><div class="ttdoc">add_edge:添加一条边 </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00066">matrixgraph.h:66</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph_html_a0fac196d4c47c2855267eb2cf888ef14"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_matrix_graph.html#a0fac196d4c47c2855267eb2cf888ef14">IntroductionToAlgorithm::GraphAlgorithm::MatrixGraph::add_edges</a></div><div class="ttdeci">void add_edges(const Iteator &amp;begin, const Iteator &amp;end)</div><div class="ttdoc">add_edges:添加一组边 </div><div class="ttdef"><b>Definition:</b> <a href="matrixgraph_8h_source.html#l00087">matrixgraph.h:87</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_8ed9dba4cd616b67da9b1338594e34e0.html">src</a></li><li class="navelem"><a class="el" href="dir_aa0a5ddea82682e3e6fbdbaa3337ee0e.html">graph_algorithms</a></li><li class="navelem"><a class="el" href="dir_7ed7dee3af3b488a5e053ec9bcb71f0e.html">basic_graph</a></li><li class="navelem"><a class="el" href="dir_d12496f1fc7378ee9aea361c57814de0.html">graph_representation</a></li><li class="navelem"><a class="el" href="dir_9818f9cf1740abb9d858846dbb9d8b6f.html">matrix_graph</a></li><li class="navelem"><a class="el" href="matrixgraph_8h.html">matrixgraph.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
  </ul>
</div>
</body>
</html>
